
package com.orange.startupwizard;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.orange.startupwizard.common.Constant;
import com.orange.startupwizard.pil.PlatformFeatureMananger;
import com.orange.startupwizard.util.AppSettingStatusUtil;
import com.orange.startupwizard.util.BitmapTools;
import com.orange.startupwizard.util.Logger;
import com.orange.startupwizard.util.OCDialogManager;
import com.orange.startupwizard.util.Util;
import com.orange.startupwizard.view.CamPreview;
import com.orange.startupwizard.view.FocusRectangle;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class WallpaperWizardCamera extends Activity implements OnClickListener {
    private static final String TAG = WallpaperWizardCamera.class.getSimpleName();
    private static final int FOCUS_START = 0;
    private static final int FOCUS_END = 1;
    private static final int defaultPictureRotation = 90;
    private final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
    private CamPreview mPreview;
    private Camera mCamera;
    private View mButtonTakeShot;
    private Button mButtonTryAgain;
    private Button mButtonOk;
    private View mButtons;
    private ImageView mImgPreview;
    private TextView mBtnsTitle;
    private boolean mCameraShotTaken = false;
    private FocusRectangle mFocusRectangle;
    private Bitmap mLastImage;
    private int mLastImageRotation;
    private int mFocusStatus = FOCUS_START;
    private boolean isInPictureTaking = false;
    private boolean needPauseCamera = false;
    private static final String SET_LOCKSCREEN_WALLPAPAER_FROM_URI = "com.orange.lockscreen.settings.setLockScreenWallpaperFromUri";
    private static final String WALLPAPER_URI = "wallpaperUri";
    private static final int CROP_PICTURE = 99;
    private Dialog dialog;

    /**
     * Called when the activity is first created.
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Util.isTablet(this))
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        initUI();
        Log.d(TAG, "onCreate'd");

        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.DIALOG_RETURN_RESULT_BROADCAST_WALLPAPER);
        registerReceiver(mViewReceiver, filter);

        IntentFilter filterFinish = new IntentFilter();
        filterFinish.addAction(Constant.ACTION_FINISH_ALL_ACTIVITY);
        registerReceiver(mIntentReceiver, filterFinish);
        WallpaperWizard.setWallpaperWithCameraFlag = true;
    }

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        public void onReceive(android.content.Context context, android.content.Intent intent) {
            finish();
        };
    };

    private BroadcastReceiver mViewReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Constant.DIALOG_RETURN_RESULT_BROADCAST_WALLPAPER)) {
                int flag = intent.getIntExtra(Constant.DIALOG_RETURN_KEY_WALLPAPER, 0);
                switch (flag) {
                    case 1:
                        // WallpaperWizard.setHomeAndLockFlag = false;
                        AppSettingStatusUtil.setCreateHomeAndLockFlag(getApplicationContext(),
                                false);
                        setHomeScreenWallpaper();
                        break;
                    case 2:
                        Intent i = new Intent(
                                SET_LOCKSCREEN_WALLPAPAER_FROM_URI);
                        i.putExtra(WALLPAPER_URI, saveImage().toString());
                        sendBroadcast(i);
                        finish();
                        break;
                    case 3:
                        // WallpaperWizard.setHomeAndLockFlag = true;
                        AppSettingStatusUtil
                                .setCreateHomeAndLockFlag(getApplicationContext(), true);
                        setHomeScreenWallpaper();
                        break;
                    default:
                        break;
                }
            }

        }
    };

    public void initUI()
    {
        setContentView(R.layout.wizard_wallpaper_cam);
        getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
                LayoutParams.FLAG_FULLSCREEN);

        mPreview = (CamPreview) findViewById(R.id.campreview);
        mImgPreview = (ImageView) findViewById(R.id.photopreview);
        mFocusRectangle = (FocusRectangle) findViewById(R.id.focus_rectangle);
        mButtons = findViewById(R.id.buttons);
        mButtonTakeShot = findViewById(R.id.buttonShot);
        mButtonTakeShot.setOnClickListener(this);
        mButtonTryAgain = (Button) findViewById(R.id.buttonTryAgain);
        mButtonTryAgain.setOnClickListener(this);
        mButtonOk = (Button) findViewById(R.id.buttonDone);
        mButtonOk.setOnClickListener(this);

        mBtnsTitle = (TextView) findViewById(R.id.title);
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (mCamera == null) {
            try {
                openCamera();
            } catch (Exception e) {
                Logger.exception(TAG, "onConfigurationChanged()", e);
            }
        }

        initUI();

        if (mCamera != null) {
            if (mPreview == null)
                mPreview = (CamPreview) findViewById(R.id.campreview);

            mPreview.setCamera(mCamera);
        }

        if (mCameraShotTaken == true)
        {
            int rot = getRotationInDegrees();
            mLastImage = rotateImgByOrientation(mLastImage, rot, mLastImageRotation);
            mLastImageRotation = rot;
            mImgPreview.setImageBitmap(mLastImage);
            displayImgMode();
        }

        updateOperatorStatus();

    }

    public static boolean hasStorage(boolean requireWriteAccess) {
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        } else if (!requireWriteAccess
                && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    public void onPause() {
        super.onPause();
        if (dialog != null) {
            dialog.dismiss();
        }
        if (!isInPictureTaking && mCamera != null) {
            pauseCamera();
        } else {
            needPauseCamera = true;
        }
    }

    private void pauseCamera() {
        if (mCamera != null) {
            mPreview.setVisibility(View.GONE);
            mPreview.setCamera(null);
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mCamera != null) {
            if (mPreview == null)
                mPreview = (CamPreview) findViewById(R.id.campreview);

            mPreview.setCamera(mCamera);
        }
    }

    public void onResume() {
        super.onResume();
        Logger.d("wall", "WallpaperWizardCamera.onResume() ");
        needPauseCamera = false;
        isInPictureTaking = false;

        if (mCamera == null) {
            try {
                openCamera();
            } catch (Exception e) {
                Logger.exception(TAG, "onResume()", e);
            }
        }

        if (mCamera != null) {
            if (mPreview == null)
                mPreview = (CamPreview) findViewById(R.id.campreview);

            mPreview.setCamera(mCamera);
            mPreview.setVisibility(View.VISIBLE);
        }

        if (mCameraShotTaken == true)
        {
            int rot = getRotationInDegrees();
            mLastImage = takePhoto(mLastImage, rot, mLastImageRotation);
            mLastImageRotation = rot;
            mImgPreview.setImageBitmap(mLastImage);
            displayImgMode();
        }
        mButtonTakeShot.setEnabled(true);
        mButtonTakeShot.setClickable(true);
        updateOperatorStatus();
        if (Util.isTablet(this))
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

    // Called when shutter is opened
    private ShutterCallback shutterCallback = new ShutterCallback() { // <6>
        public void onShutter() {
            isInPictureTaking = true;
            playShutterSound();
            Log.d(TAG, "onShutter'd");
        }
    };

    private void playShutterSound() {
        SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
        int shutterSound = soundPool.load(WallpaperWizardCamera.this, R.raw.camera_click, 0);
        soundPool.play(shutterSound, 1f, 1f, 0, 0, 1);
        soundPool.release();
    }

    // Handles data for jpeg picture
    private PictureCallback jpegCallback = new PictureCallback() { // <8>
        public void onPictureTaken(byte[] data, Camera camera) {
            mLastImage = BitmapTools.decodeByteArray(data, 0, data.length);
            mCameraShotTaken = true;
            mLastImageRotation = getRotationInDegrees();
            mLastImage = takePhoto(mLastImage, mLastImageRotation, defaultPictureRotation);
            mImgPreview.setImageBitmap(mLastImage);
            Log.d(TAG, "onPictureTaken - jpeg");
            displayImgMode();
            mButtonTakeShot.setEnabled(true);
            mButtonTakeShot.setClickable(true);
            isInPictureTaking = false;
            if (needPauseCamera) {
                pauseCamera();
                needPauseCamera = false;
            }
            new Thread() {
                public void run() {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                        if (Util.isTablet(WallpaperWizardCamera.this))
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        else
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                        // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                    } catch (InterruptedException e) {
                        Log.i(TAG, "jpegCallback", e);
                    }
                };
            }.start();
        }

    };

    private int getRotationInDegrees()
    {
        int degrees = 0;
        WindowManager wm = getWindowManager();
        // int rotation = wm.getDefaultDisplay().getRotation();
        int rotation = 0;
        if (sdkVersion < Build.VERSION_CODES.FROYO) {
            rotation = wm.getDefaultDisplay().getOrientation();
        } else {
            rotation = wm.getDefaultDisplay().getRotation();
        }
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }
        return degrees;
    }

    private Bitmap takePhoto(Bitmap b, int rotation, int prr) {
        if (b == null)
            return null;

        int degrees = 0;
        int w = mLastImage.getWidth();
        int h = mLastImage.getHeight();

        degrees = prr - rotation;

        Matrix m = new Matrix();
        m.postRotate(degrees);

        Bitmap res = null;
        res = Bitmap.createBitmap(b, 0, 0,
                w, h, m, true);
        return res;
    }

    private Bitmap rotateImgByOrientation(Bitmap b, int rotation, int prr) {
        if (b == null)
            return null;

        int degrees = 0;
        int w = mLastImage.getWidth();
        int h = mLastImage.getHeight();

        degrees = prr - rotation;

        Matrix m = new Matrix();
        m.postRotate(degrees);

        Bitmap res = null;
        res = Bitmap.createBitmap(b, 0, 0,
                w, h, m, true);
        if (mLastImage != null && !mLastImage.isRecycled()) {
            mLastImage.recycle();
        }
        return res;
    }

    public void onClick(View v) {
        if (v == mButtonTakeShot) {
            if (!mCameraShotTaken) {
                if (Util.isPortraitMode(WallpaperWizardCamera.this)) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else if (getRotationInDegrees() != 270
                        && !Util.isTablet(WallpaperWizardCamera.this)) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
                if (mCamera != null) {
                    mButtonTakeShot.setEnabled(false);
                    mButtonTakeShot.setClickable(false);
                    mCamera.takePicture(shutterCallback, null, jpegCallback);
                }
            }
        } else if (v == mButtonTryAgain) {
            if (mCameraShotTaken) {
                mCameraShotTaken = false;
                if (mLastImage != null && !mLastImage.isRecycled()) {
                    mLastImage.recycle();
                }
                displayPreviewMode();
            }
        } else if (v == mButtonOk) {
            if (mCameraShotTaken) {
                if (Util.isHaveLockscreen(getApplicationContext())
                        && android.os.Build.VERSION.SDK_INT < 17) {
                    String[] items = {
                            getString(R.string.homescreen_wallpaper),
                            getString(R.string.lockscreen_wallpaper),
                            getString(R.string.both)
                    };

                    dialog = OCDialogManager.createOUDialog(this,
                            R.string.wallpaper_choose_list_title, items,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which) {
                                        case 0:
                                            if (AppSettingStatusUtil
                                                    .isUseHomeScreenWallpaper(getBaseContext())
                                                    && Util.isLiveWallPaper(getBaseContext())) {
                                                OCDialogManager
                                                        .createAlertDlg(
                                                                WallpaperWizardCamera.this,
                                                                getString(R.string.label_set_wallpaper),
                                                                getString(R.string.msg_set_wallpaper),
                                                                new android.content.DialogInterface.OnClickListener() {

                                                                    @Override
                                                                    public void onClick(
                                                                            DialogInterface dialog,
                                                                            int which) {
                                                                        // TODO
                                                                        // Auto-generated
                                                                        // method
                                                                        // stub
                                                                        // WallpaperWizard.setHomeAndLockFlag
                                                                        // =
                                                                        // false;
                                                                        AppSettingStatusUtil
                                                                                .setCreateHomeAndLockFlag(
                                                                                        getApplicationContext(),
                                                                                        false);
                                                                        setHomeScreenWallpaper();
                                                                    }
                                                                }, null).show();
                                            } else {

                                                // WallpaperWizard.setHomeAndLockFlag
                                                // = false;
                                                AppSettingStatusUtil.setCreateHomeAndLockFlag(
                                                        getApplicationContext(), false);
                                                setHomeScreenWallpaper();

                                            }
                                            break;
                                        case 1:
                                            finish();
                                            Intent intent = new Intent(
                                                    SET_LOCKSCREEN_WALLPAPAER_FROM_URI);
                                            intent.putExtra(WALLPAPER_URI, saveImage().toString());
                                            sendBroadcast(intent);
                                            break;
                                        case 2:
                                            // WallpaperWizard.setHomeAndLockFlag
                                            // = true;
                                            AppSettingStatusUtil.setCreateHomeAndLockFlag(
                                                    getApplicationContext(), true);
                                            setHomeScreenWallpaper();
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            });

                    // OUDialogBuilder builder = new OUDialogBuilder(
                    // this);
                    // builder
                    // .setTitle(R.string.wallpaper_choose_list_title)
                    // .setItems(items,new DialogInterface.OnClickListener() {
                    // @Override
                    // public void onClick(DialogInterface dialog, int which) {
                    // switch (which) {
                    // case 0:
                    // WallpaperWizard.setHomeAndLockFlag = false;
                    // setHomeScreenWallpaper();
                    // break;
                    // case 1:
                    // finish();
                    // Intent intent = new Intent(
                    // SET_LOCKSCREEN_WALLPAPAER_FROM_URI);
                    // intent.putExtra(WALLPAPER_URI, saveImage().toString());
                    // sendBroadcast(intent);
                    // break;
                    // case 2:
                    // WallpaperWizard.setHomeAndLockFlag = true;
                    // setHomeScreenWallpaper();
                    // break;
                    // default:
                    // break;
                    // }
                    // }
                    // } );
                    // AlertDialog dialog = builder.create();
                    dialog.show();
                } else {
                    AppSettingStatusUtil.setChooseHomeAndLockFlag(
                            getApplicationContext(), true);
                    setHomeScreenWallpaper();
                }
            }
        }
    }

    private void setHomeScreenWallpaper() {
        if (Util.isPortraitMode(WallpaperWizardCamera.this)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else if (getRotationInDegrees() != 270 && !Util.isTablet(WallpaperWizardCamera.this)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        new Thread() {
            public void run() {
                Uri data = saveImage();
                // Util.setWallpaperImage(data, WallpaperWizardCamera.this);
                // finish();
                int width = getWallpaperDesiredMinimumWidth();
                int height = getWallpaperDesiredMinimumHeight();
                Display display = getWindowManager().getDefaultDisplay();
                float spotlightX = (float) display.getWidth() / width;
                float spotlightY = (float) display.getHeight() / height;
                Intent intent = new Intent("com.android.camera.action.CROP")
                        .setDataAndType(data, "image/*")
                        .putExtra("aspectX", width)
                        .putExtra("aspectY", height)
                        .putExtra("spotlightX", spotlightX)
                        .putExtra("spotlightY", spotlightY)
                        .putExtra("scale", true);
                // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivityForResult(intent, CROP_PICTURE);
            };
        }.start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CROP_PICTURE:
                    if (data != null) {
                        Uri uri = data.getData();
                        if (uri == null && data.getAction() != null) {
                            uri = Uri.parse(data.getAction());
                        }
                        if (uri != null) {
                            Util.setWallpaperImage(uri, this);
                            finish();
                        }
                    }
            }
        }
    }

    private void displayPreviewMode() {
        if (Util.DEVICE_NAME_KUMQUATFEATURE.equals(PlatformFeatureMananger.getPlatformFeature()
                .getDeviceName())) {
            needPauseCamera = false;
            isInPictureTaking = false;

            if (mCamera == null) {
                try {
                    openCamera();
                } catch (Exception e) {
                    Logger.exception(TAG, "onResume()", e);
                }
            }

            if (mCamera != null) {
                if (mPreview == null)
                    mPreview = (CamPreview) findViewById(R.id.campreview);

                mPreview.setCamera(mCamera);
                mPreview.setVisibility(View.VISIBLE);
            }

            mButtonTakeShot.setEnabled(true);
            mButtonTakeShot.setClickable(true);
            updateOperatorStatus();
            if (Util.isTablet(WallpaperWizardCamera.this))
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            else
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
            // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }
        if (mCamera != null) {
            mCamera.startPreview();
            mButtons.setVisibility(View.GONE);
            mButtonTakeShot.setVisibility(View.VISIBLE);
            mImgPreview.setVisibility(View.GONE);
            mBtnsTitle.setText(R.string.wallpaper_cam_shot_title);
            mFocusStatus = FOCUS_START;
            updateOperatorStatus();
        }
    }

    private void displayImgMode() {
        mButtons.setVisibility(View.VISIBLE);
        mButtonTakeShot.setVisibility(View.GONE);
        mImgPreview.setVisibility(View.VISIBLE);
        mBtnsTitle.setText(R.string.wallpaper_cam_shot_taken_title);
        if (Util.DEVICE_NAME_KUMQUATFEATURE.equals(PlatformFeatureMananger.getPlatformFeature()
                .getDeviceName())) {
            pauseCamera();
        }
        if (mCamera != null) {
            mCamera.stopPreview();
        }
        mFocusStatus = FOCUS_END;
        updateOperatorStatus();
    }

    private Uri saveImage() {
        if (mLastImage == null)
            return null;
        Uri resUri = null;
        String filename = null;
        Cursor imgCursor = null;
        try {
            filename = Long.toString(System.currentTimeMillis());

            // Bitmap resizedBitmap =
            // BitmapFactory.decodeByteArray(mLastImageData, 0,
            // mLastImageData.length);
            String imgPath = MediaStore.Images.Media.insertImage(getContentResolver(), mLastImage,
                    filename, filename);
            // resizedBitmap.recycle();

            if (imgPath != null) {
                // if image is ready, we need to update media library
                // with new item
                resUri = Uri.parse(imgPath);
                imgCursor = getContentResolver().query(resUri, null, null,
                        null, null);
                if (imgCursor != null) {
                    imgCursor.moveToFirst();
                    String filePath = imgCursor.getString(imgCursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));

                    if (filePath != null) {
                        Uri fileUri = Uri.parse(ContentResolver.SCHEME_FILE + "://" + filePath);
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));
                    }
                } else {
                    // in case we cannot get file name,
                    // lets notify in different way
                    sendBroadcast(new Intent(
                            Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse(ContentResolver.SCHEME_FILE + "://"
                                    + Environment.getExternalStorageDirectory())));
                }
            }

        } catch (Exception ex) {
            Logger.e(TAG, "failed to save img");
        } finally {
            if (imgCursor != null)
                imgCursor.close();
        }
        return resUri;
    }

    private void updateOperatorStatus() {

        if (!hasStorage(true)) {
            mButtonTakeShot.setEnabled(false);
            mButtonTakeShot.setFocusable(false);
            mBtnsTitle.setText(R.string.wallpaper_cam_no_card);
        } else {
            mButtonTakeShot.setEnabled(true);
            mButtonTakeShot.setFocusable(true);
        }

        updateFocusIndicator();
    }

    private void updateFocusIndicator() {
        switch (mFocusStatus) {
            case FOCUS_START:
                mFocusRectangle.showStart();
                break;
            default:
                mFocusRectangle.clear();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_SEARCH:
            case KeyEvent.KEYCODE_MENU:
                if (event.isLongPress()) {
                    return true;
                }
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    // added by ycc 2011/10/25
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        WallpaperWizard.setWallpaperWithCameraFlag = false;
        unregisterReceiver(mViewReceiver);
        unregisterReceiver(mIntentReceiver);
        if (mLastImage != null && !mLastImage.isRecycled()) {
            mLastImage.recycle();
        }
    }

    // end

    // private void openCamera() {
    // mCamera = Camera.open();
    // Camera.Parameters params = mCamera.getParameters();
    // List<String> focusModes = params.getSupportedFocusModes();
    // if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
    // {
    // params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    // }
    // mCamera.setParameters(params);
    // }

    private void openCamera() {
        mCamera = Camera.open();
        Camera.Parameters params = mCamera.getParameters();
        List<String> focusModes = params.getSupportedFocusModes();
        if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }

        List<Size> sizes = params.getSupportedPictureSizes();

        for (int i = 0; i < sizes.size(); i++) {
            Logger.i("test1", "width : " + sizes.get(i).width + " height : " + sizes.get(i).height);
        }

        Size mSize = sizes.get(sizes.size() / 2);
        params.setPictureSize(mSize.width, mSize.height);
        Logger.i("test1", "params get picture size : " + params.getPictureSize().height + " : "
                + params.getPictureSize().width);
        mCamera.setParameters(params);
    }
}
